package cn.iocoder.yudao.module.pplan.controller.admin.serviceequipment;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pplan.controller.admin.serviceequipment.vo.ServiceEquipmentPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.serviceequipment.vo.ServiceEquipmentRespVO;
import cn.iocoder.yudao.module.pplan.controller.admin.serviceequipment.vo.ServiceEquipmentSaveReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.serviceequipment.ServiceEquipmentDO;
import cn.iocoder.yudao.module.pplan.service.serviceequipment.ServiceEquipmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 生产计划-服务用具管理")
@RestController
@RequestMapping("/pplan/service-equipment")
@Validated
public class ServiceEquipmentController {

    @Resource
    private ServiceEquipmentService serviceEquipmentService;

    @PostMapping("/create")
    @Operation(summary = "创建生产计划-服务用具管理")
    @PreAuthorize("@ss.hasPermission('pplan:service-equipment:create')")
    public CommonResult<Long> createServiceEquipment(@Valid @RequestBody ServiceEquipmentSaveReqVO createReqVO) {
        return success(serviceEquipmentService.createServiceEquipment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产计划-服务用具管理")
    @PreAuthorize("@ss.hasPermission('pplan:service-equipment:update')")
    public CommonResult<Boolean> updateServiceEquipment(@Valid @RequestBody ServiceEquipmentSaveReqVO updateReqVO) {
        serviceEquipmentService.updateServiceEquipment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产计划-服务用具管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pplan:service-equipment:delete')")
    public CommonResult<Boolean> deleteServiceEquipment(@RequestParam("id") Long id) {
        serviceEquipmentService.deleteServiceEquipment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除生产计划-服务用具管理")
                @PreAuthorize("@ss.hasPermission('pplan:service-equipment:delete')")
    public CommonResult<Boolean> deleteServiceEquipmentList(@RequestParam("ids") List<Long> ids) {
        serviceEquipmentService.deleteServiceEquipmentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产计划-服务用具管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pplan:service-equipment:query')")
    public CommonResult<ServiceEquipmentRespVO> getServiceEquipment(@RequestParam("id") Long id) {
        ServiceEquipmentDO serviceEquipment = serviceEquipmentService.getServiceEquipment(id);
        return success(BeanUtils.toBean(serviceEquipment, ServiceEquipmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产计划-服务用具管理分页")
    @PreAuthorize("@ss.hasPermission('pplan:service-equipment:query')")
    public CommonResult<PageResult<ServiceEquipmentRespVO>> getServiceEquipmentPage(@Valid ServiceEquipmentPageReqVO pageReqVO) {
        PageResult<ServiceEquipmentDO> pageResult = serviceEquipmentService.getServiceEquipmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceEquipmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产计划-服务用具管理 Excel")
    @PreAuthorize("@ss.hasPermission('pplan:service-equipment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportServiceEquipmentExcel(@Valid ServiceEquipmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceEquipmentDO> list = serviceEquipmentService.getServiceEquipmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "服务用具列表.xls", "数据", ServiceEquipmentRespVO.class,
                        BeanUtils.toBean(list, ServiceEquipmentRespVO.class));
    }

}